package com.copro.chemdb.stat;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ihelpuoo.lang.NumericUtils;

import com.copro.chemdb.stat.rule.AccumulateStatRule;
import com.copro.chemdb.subject.Subject;
import com.copro.chemdb.subject.SubjectUtils;
import com.copro.simple.Application;
import com.copro.simple.SimpleException;

public class ExportStatAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Application app = Application.me(request);
		int quesId = NumericUtils.parseInt(request.getParameter("quesId"), -1);
		int statType = NumericUtils.parseInt(request.getParameter("statType"),
				1);
		String ids = request.getParameter("ids");
		String[] subjIds = ids.split(",");
		Subject subject = null;
		// set the response header info for download the excel
		response.setContentType("application/unknown;charset=utf-8");
		response.addHeader("content-disposition",
				"attachment;filename=statistic.xls");
		response.setHeader("Pragma", "No-Cache");
		response.setHeader("Cache-Control", "No-Cache");
		response.setDateHeader("Expires", 0);

		// create the workbook
		HSSFWorkbook wb = new HSSFWorkbook();
		// create three common style
		HSSFCellStyle styleHeader = wb.createCellStyle();
		styleHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		styleHeader.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		HSSFCellStyle styleInt = wb.createCellStyle();
		styleInt.setDataFormat((short) 1);
		HSSFCellStyle stylePercent = wb.createCellStyle();
		stylePercent.setDataFormat((short) 0xa);
		HSSFCellStyle styleHeaderInt = wb.createCellStyle();
		styleHeaderInt.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		styleHeaderInt.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		styleHeaderInt.setDataFormat((short) 1);
		HSSFCellStyle styleHeaderPercent = wb.createCellStyle();
		styleHeaderPercent.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		styleHeaderPercent
				.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		styleHeaderPercent.setDataFormat((short) 0xa);

		short customStyle = styleHeader.getIndex();

		for (int i = 0; i < subjIds.length; i++) {
			subject = new Subject();
			subject.setId(NumericUtils.parseInt(subjIds[i], -1));
			subject.setQuesId(quesId);
			exportRule(app, subject, wb, statType, customStyle);
		}
		wb.write(response.getOutputStream());
		response.flushBuffer();
		return null;
	}

	private void exportRule(Application app, Subject subject, HSSFWorkbook wb,
			int statType, short customStyle) throws SimpleException {
		subject = SubjectUtils.get(app, subject);
		if (subject.isHasOptions()) {
			subject.setOptions(SubjectUtils.queryOptions(app, subject));
		}
		StatRule rule = app.getStatRule(subject.getRule());
		if (rule instanceof AccumulateStatRule) {
			List<Subject> children = SubjectUtils.queryChildrenSubjects(app,
					subject, statType);
			Iterator<Subject> it = children.iterator();
			while (it.hasNext()) {
				Subject child = it.next();
				if (child.isHasOptions())
					child.setOptions(SubjectUtils.queryOptions(app, child));
				StatRule childRule = app.getStatRule(child.getRule());
				if (null == childRule)
					continue;
				exportRule(app, child, wb, statType, customStyle);
			}
		} else {
			StatResult re = rule.doStat(app, subject, statType);
			doExport(app, wb, subject, re, customStyle);
		}
	}

	private void doExport(Application app, HSSFWorkbook wb, Subject subject,
			StatResult re, short customStyle) throws SimpleException {
		ExportRule rule = app.getExportRule(subject.getRule());
		rule.doExport(wb, subject, re, customStyle, app);
	}
}
